<?php

require_once 'common/conn.php';

if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}

if (isset($_GET['username'])) {
    $username = $_GET['username'];
}

if (isset($_GET['password'])) {
    $password = $_GET['password'];
}
$action = $_GET['action'];
if (!isset($action)) {
    $action = $_POST['action'];
}

$result = array();

$db = new DBC();
$is_exist = false;
if ($action == "login") {
    if (isset($username) && isset($password)) {
        $sql = "SELECT devicecode AS usercode FROM t_device WHERE (email = '" . $username . "' OR nickname = '" . $username . "') AND password = '" . $password . "'";
    } else {
        $sql = "SELECT devicecode AS usercode FROM t_device WHERE devicecode = '" . $usercode . "'";
    }

    $data = $db->fetchData($sql);
    if ($data != null && isset($data) && count($data) > 0) {
        $usercode = $data[0]['usercode'];
        $is_exist = true;
    }
    
    if ($is_exist) {
        $result['errorNo'] = "0";
        $sql = "SELECT * FROM t_login_record WHERE usercode = '" . $usercode . "' AND (TO_DAYS(logintime) = TO_DAYS(now()))";

        $login_array = $db->fetchData($sql);
        if ($login_array != false && count($login_array) > 0) {
            $result['everyday'] = "0";
            $result['first'] = "0";
        } else {
            $result['everyday'] = "1";
            $sql = "SELECT * FROM t_login_record WHERE usercode = '" . $usercode . "'";
            $login_array = $db->fetchData($sql);
            if ($login_array != false && count($login_array) > 0) {
                $result['first'] = "0";
            } else {
                $result['first'] = "1";
            }
        }
        // type=1 login
        $sql = "INSERT INTO t_login_record(usercode,logintime,type) VALUES('" . $data[0]['usercode'] . "',now(),1)";
        $db->execute($sql);

        $sql = "SELECT DATE(logintime) AS logindate FROM t_login_record WHERE usercode = '" . $usercode . "' GROUP BY DATE(logintime) ORDER BY DATE(logintime) DESC";
        $data = $db->fetchData($sql);
        $days = 1;
        $run = true;
        while ($run) {
            $ago = "-" . $days . " day";
            $day = date("Y-m-d", strtotime($ago));
            $logon = false;
            foreach ($data as $row) {
                if ($row['logindate'] == $day) {
                    $logon = true;
                }
            }
            if ($logon) {
                $days++;
            } else {
                $run = false;
            }
        }
        $result['lastdays'] = "".$days."";
    } else {
        $result['errorNo'] = "1";
        if(isset($username) && isset($password)){
            $result['errorMsg'] = "用户名或密码不正确";
        }else{
            $result['errorMsg'] = "用户不存在";
        }
    }
} else if ($action == "logout") {
    // type=-1 logout
    $sql = "INSERT INTO t_login_record(usercode,logintime,type) VALUES('" . $usercode . "',now(),-1)";
    if ($db->execute($sql)) {
        $result['errorNo'] = "0";
    } else {
        $result['errorNo'] = "1";
        $result['errorMsg'] = "退出登录失败";
    }
}

echo json_encode($result);
?>